﻿// <copyright file="ModemOn.cs" company="Hallvard Korsgaard">
//  Microsoft Public License (Ms-PL). See license details here: http://enoceannet.codeplex.com/license
// </copyright>
// <author>Hallvard Korsgaard</author>

namespace EnOcean.Command
{
  using System;

  /// <summary>
  /// Activates TCM modem functionality and sets the modem ID. An OK confirmation telegram is generated. The modem ID is the ID
  /// at which the TCM receives messages of type 6DT. The modem ID and modem status (ON/OFF) is stored in EEPROM.
  /// The modem ID range is from 0x0001 to 0xFFFF. IF 0x0000 is provided as modem ID, the modem is activated with the ID previously stored in EEPROM.
  /// </summary>
  [Serializable]
  public class ModemOn : CommandTelegram
  {
    /// <summary>
    /// Variable for the ModemId property.
    /// </summary>
    private int modemId;

    /// <summary>
    /// Initializes a new instance of the ModemOn class. The modem id is set to 0x0000, that will activate the modem with the ID previously stored in EEPROM.
    /// </summary>
    public ModemOn() : this(0)
    {
    }
    
    /// <summary>
    /// Initializes a new instance of the ModemOn class.
    /// </summary>
    /// <param name="modemId">The modem ID range is from 0x0001 to 0xFFFF.</param>
    public ModemOn(int modemId)
    {
      this.ModemId = modemId;
    }

    /// <summary>
    /// Gets or sets the modem id. The id is a 16 bit value in the range 0 to 0xffff inclusive.
    /// </summary>
    public int ModemId
    {
      get
      {
        return this.modemId;
      }

      set
      {
        if (value >= 0x0 && value <= 0xffff)
        {
          this.modemId = value;
        }
        else
        {
          throw new ArgumentOutOfRangeException("ModemId", "must be a 16 bit value in the range 0x0 to 0xffff inclusive");
        }
      }
    }

    /// <summary>
    /// Creates the byte format of the message.
    /// </summary>
    /// <param name="message">A byte array of length 14 to hold the message contents.</param>
    protected internal override void CreateMessage(byte[] message)
    {
      base.CreateMessage(message);
      message[3] = (byte)CommandType.ModemOn;
      message[4] = (byte)(this.modemId >> 8);
      message[5] = (byte)(this.modemId & 0xff);
    }
  }
}
